package com.zy.rpc.common.Serializer;

import com.zy.rpc.common.Serializer.impl.JavaSerializer;
import com.zy.rpc.common.Serializer.impl.JsonSerializer;
import com.zy.rpc.common.Serializer.impl.ProtobufSerializer;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import java.util.HashMap;

/**
 * @author zy
 * @date 2023/5/24 18:46
 */
public class SerializerFactory {

    private static final HashMap<String, Serializer> map = new HashMap<>();
    @Value("${serializer.defaultSerializerName}")
    private static String defaultSerializerName;
    @PostConstruct
    private void init() {
        map.put("json", new JsonSerializer());
        map.put("java", new JavaSerializer());
        map.put("protobuf", new ProtobufSerializer());
    }
     private static   Serializer getSerializer(String serializerName) {
        Serializer serializer = map.get(serializerName);
        if (serializer == null) {
            throw new IllegalArgumentException("Unknown serialization type: " + serializerName);
        }
        return serializer;
    }
    public static Serializer getDefaultSerializer() {
        return getSerializer(defaultSerializerName);
    }

    public static void registerCustomSerializer(String name, Serializer customSerializer) {
        map.put(name, customSerializer);
    }
}
